﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using App.Web.Utility;

namespace App.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            var nameFormat = @"[\-\w\d]+";
            //var actionFormat = @"\w+";
            
            routes.MapRoute(
                AppConstants.RouteNames.Public.ITEM_DETAILS,
                "product/details/{id}/{productKey}", 
                new { action = "Details", controller = "Product", id = UrlParameter.Optional }, 
                new { productKey = nameFormat }, 
                new String[] {"App.Web.Controllers"} 
            );
            routes.MapRoute(
                AppConstants.RouteNames.Public.COMPANY_DETAILS,
                "{CompanyKey}",
                new { controller = "Company", action = "Details" },
                new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes) },
                new String[] { "App.Web.Controllers" }
            );
            routes.MapRoute(
                AppConstants.RouteNames.Public.COMPANY_PRODUCT_DETAILS,
                "{CompanyKey}/{id}/{productKey}",
                //new { controller = "Company", action = "ProductDetails" },
                new {controller = "Product", action="Details"},
                new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes), ProductKey = nameFormat },
                new String[] { "App.Web.Controllers" }
            );
            routes.MapRoute(
                AppConstants.RouteNames.Public.CONTENT_DETAILS, 
                "{ContentUrl}", 
                new { controller = "Content", action = "ViewContent" }, 
                new { ContentUrl = String.Format("({0})", ApplicationCache.ContentRoutes) }, 
                new String[] { "App.Web.Controllers" }
            );            
            routes.MapRoute(
                AppConstants.RouteNames.Public.DEFAULT, // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new String[] { "App.Web.Controllers" } 
            );
        }

        protected void Application_Start()
        {            
            //AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RefreshRoutes(false);
            //RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
            ///Configure Auto Mapper
            App.Web.Utility.BootStrapper.ConfigureTypeMappings();

            App.Web.Utility.BootStrapper.SetUserRoles();
            App.Web.Utility.BootStrapper.PrepareDomainUtility();
            App.Core.Mail.MailManager.SmtpHost = App.Web.Utility.ConfigReader.SmtpHost;
            App.Core.Mail.MailManager.SmtpPort = App.Web.Utility.ConfigReader.SmtpPort;
            //App.Web.Utility.LuceneEngine.LuceneIndexDirectory = App.Web.Utility.ConfigReader.LuceneIndexDirectory;
            //App.Web.Utility.LuceneEngine.BuildSearchIndex();
        }
        protected void Application_Error(Object sender, EventArgs e)
        {
            BootStrapper.SendErrorEmail(Server.GetLastError());
        }

        public static void RefreshRoutes(bool forceRebuild)
        {
            if (RouteTable.Routes.Count > 0)
                RouteTable.Routes.Clear();
            
            ///Prepare the cache of URL Routes
            ApplicationCache.RefreshContentRouteCache(forceRebuild);
            ApplicationCache.RefreshCompanyRouteCache(forceRebuild);

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
    }
}